<template>
  <div  class="register">
    <el-form :model="registerForm" ref="registerForm" :rules="registerRules" label-position="left" class="register-form">
      <h3 class="title">用户注册</h3>
      <el-form-item prop="name"label="名称">
        <el-input type="text" v-model="registerForm.name" auto-complete="off" placeholder="请输入姓名！"></el-input>
      </el-form-item>
      <el-form-item prop="sex"label="性别">
        <el-select v-model="registerForm.sex" placeholder="请选择" style="width: 350px">
          <el-option label="男" :value="1"></el-option>
          <el-option label="女" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="phone"label="电话">
        <el-input type="text" v-model="registerForm.phone" auto-complete="off" placeholder="请输入电话！"></el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="registerForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleUserRegister"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="register-code">
          <img :src="codeUrl" @click="getCode" class="register-code-img"/>
        </div>
      </el-form-item>
      <el-form-item prop="name"label="微信">
        <el-input type="text" v-model="registerForm.weChat" auto-complete="off" placeholder="请输入微信！"></el-input>
      </el-form-item>
      <el-form-item prop="name"label="小区">
        <el-input type="text" v-model="registerForm.community" auto-complete="off" placeholder="请输入小区！"></el-input>
      </el-form-item>
      <el-form-item prop="name"label="楼栋">
        <el-input type="text" v-model="registerForm.building" auto-complete="off" placeholder="请输入楼栋！"></el-input>
      </el-form-item>
      <el-form-item prop="name"label="住址">
        <el-input type="text" v-model="registerForm.address" auto-complete="off" placeholder="请输入住址！"></el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:100%;" @click.native.prevent="handleUserRegister" >入驻</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getCodeImg, addUser } from "@/api/login";

export default {
  name: "UserRegister",
  data() {
    return {
      codeUrl: "",
      registerForm: {
          name:'',
          sex:1,
          phone:'',
          weChat:'',
          community:'',
          building:'',
          address:'',
          code: '',
      },
      captchaEnabled: true,
      loading: false,
      registerRules: {
        name: [
          { required: true, message: '请输入姓名!', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话!', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入住址!', trigger: 'blur' }
        ],
        community: [
          { required: true, message: '请输入小区!', trigger: 'blur' }
        ],
        building: [
          { required: true, message: '请输入楼栋!', trigger: 'blur' }
        ]
      }
    };
  },
  created() {
    this.getCode();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.registerForm.uuid = res.uuid;
        }
      });
    },

    //提交注册
    handleUserRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          addUser(this.registerForm).then(res => {
            const name = this.registerForm.name;
            this.$alert("<font color='red'>恭喜你，您的账号 " + name + " 注册成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/login");
            }).catch(() => {});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          })
        }
      });
    }
  }
}

</script>
<style rel="stylesheet/scss" lang="scss">
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 38px;
}
</style>
